'use strict';

const { Service } = require('ee-core');
const os = require('os');
const {machineId, machineIdSync} = require('node-machine-id');
const {
  app: electronApp, dialog, shell, Notification, 
  powerMonitor, screen, nativeTheme
} = require('electron');
const Log = require('ee-core/log');

/**
 * device
 * @class
 */
class DeviceService extends Service {

  constructor(ctx) {
    super(ctx);
  }
  
  
  /**
   * Mac地址
   */
  getIpAndMacAddr() {
  	let mac = '';
  	// 获取网络接口信息
  	const interfaces = os.networkInterfaces();
  	 
  	// 查找第一个非本地连接的IPv4地址
  	let ip;
  	for (const key in interfaces) {
		interfaces[key].forEach(element => {
			if(element.family == "IPv4" && element.mac != "00:00:00:00:00:00"){
				ip = element.address;
				mac = element.mac;
				Log.info("key:"+ key+" ip:"+ ip + " mac:"+mac)
			}
		});
  	}
  	// if(os.networkInterfaces().WLAN){
  	// 	mac = os.networkInterfaces().WLAN[0].mac
  	// }else{
  	// 	mac = os.networkInterfaces()['以太网'][0].mac                                                                                                       
  	// }
  	return {mac, ip};
  }
  
  /**
   * 获取分辨率 1920*1080
   */
  getScreenResolution() {
  	 let res = screen.getPrimaryDisplay();
  	 return res.bounds.width + '*' + res.bounds.height
  }
  
  /**
   * 获取设备唯一编码
   */
  getMachineId() {
  	  return machineIdSync();
  }

}

DeviceService.toString = () => '[class DeviceService]';
module.exports = DeviceService;  